<template>
  <div class="articles-container">
    <div class="app-container">
      <el-card>
        <!-- 头部搜索导航栏 -->
        <el-row>
          <el-col :span="18">
            <el-form :inline="true" ref="handel" :model="requestParameters" >
              <el-form-item label="关键词">
                <el-input placeholder="根据文章标题搜索" v-model="requestParameters.keyword"></el-input>
              </el-form-item>
              <el-form-item label="状态">
                <el-select v-model="requestParameters.state" placeholder="请选择" size="small">
                  <el-option value="1" label="启用"></el-option>
                  <el-option value="0" label="禁用"></el-option>
                </el-select>
              </el-form-item>
              <el-form-item >
                  <el-button @click="clearform">清除</el-button>
                  <el-button type="primary" @click="clickSearch">搜索</el-button>
              </el-form-item>
            </el-form>
          </el-col>
          <el-col :span="6">
            <el-button class="filter-item fr" size="small"   @click="openshow" type="success"  icon="el-icon-edit" >新增技巧</el-button>
          </el-col>
        </el-row>
        <!-- 数据记录 -->
        <el-alert  :title="alerttitle"  type="info" show-icon :closable="false"></el-alert>
        <!-- 数据列表 -->
        <el-table :data="tableData">
          <el-table-column type="index" :index="indexMethod" label="序号" width="120px"></el-table-column>
          <el-table-column prop="title" label="文章标题" width="400px">
                <template #default="{row}">
               <span>{{row.title}}</span>
               <a href="#" v-if="row.videoURL" @click.prevent="handleClick(row.videoURL)">
                 <span class="el-icon-film"></span>
               </a>
            </template>
          </el-table-column>
          <el-table-column prop="visits" label="阅读数" width="243px"></el-table-column>
          <el-table-column prop="username" label="录入人" width="242px"></el-table-column>
          <el-table-column  prop="createTime"  label="录入时间" width="242px">
            <template #default={row}>
              {{formatTime(row.createTime)}}
            </template>
          </el-table-column>
          <el-table-column prop="state" label="状态" width="200px" :formatter="formatterMethod">
          </el-table-column>
          <!-- 操作按钮 -->
          <el-table-column align="center" label="操作" width="180px">
            <template #default="{row}">
              <el-button type="text" @click="showview(row)" >预览</el-button>
              <el-button type="text">
                <span v-if="row.state === 0" @click="row.state = 1">启用</span>
                <span v-else @click="row.state = 0">禁用</span>
              </el-button>
              <el-button type="text" :disabled="row.state === 1" @click="handleEdit(row)">修改</el-button>
              <el-button type="text" :disabled="row.state === 1"  @click="del(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-pagination
          style="margin-top:20px;text-align:right"
          :current-page="requestParameters.page"
          :page-sizes="[10, 20, 30, 50]"
          :page-size="requestParameters.pagesize"
          layout="prev, pager, next, sizes,  jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          background
        />
        <!-- 播放视频的对话框 -->
        <div class="box" v-if="videoShow"  >
          <div class="top">
          <div class="close" @click="closeVideo">X</div>
          <video :src="url" controls autoplay muted ref="video" ></video>
          </div>
        </div >
        <!-- 新增弹框 -->
        <ArticlesAdd  ref="Edit" :show-dialog.sync="showDialog" :titleInfo="titleInfo"  @reload="reqgetlist" ></ArticlesAdd>
        <!-- 文章预览 -->
        <ArticlesPreview :articlePreviewshow.sync="articlePreviewshow" :viewdata="viewdata"></ArticlesPreview>
      </el-card>
    </div>
  </div>
</template>

<script>
import { list, remove } from '@/api/hmmm/articles'
import ArticlesAdd from './../components/articles-add'
import { status } from '@/api/hmmm/constants'
import ArticlesPreview from './../components/articles-preview'
import dayjs from 'dayjs'
export default {
  name: 'articles',
  components: {
    ArticlesAdd,
    ArticlesPreview
  },
  data () {
    return {
      tableData: [], // 列表基本数据
      viewdata: {}, // 传输给预览数据
      requestParameters: {
        page: 1,
        pagesize: 10,
        keyword: null,
        state: null
      }, // 分页
      total: 0,
      showDialog: false, // 控制新增和修改弹框
      videoShow: false,
      articlePreviewshow: false, // 控制预览弹框
      titleInfo: '',
      alerttitle: '',
      url: ''
    }
  },
  created () {
    this.getlist()
  },
  methods: {
    // 时间格式
    formatTime (time) {
      return dayjs(time).format('YYYY-MM-DD HH:mm:ss')
    },
    // 清除
    clearform () {
      this.requestParameters = {
        page: 1,
        pagesize: 10,
        keyword: null,
        state: null
      }
      this.getlist()
    },
    // 搜索
    clickSearch () {
      this.requestParameters.page = 1
      this.getlist()
    },
    // 获取基本数据
    async getlist () {
      const { data } = await list(this.requestParameters)
      this.tableData = data.items
      this.total = data.counts
      this.alerttitle = `数据共 ${this.total} 条记录`
    },
    // 列表显示启用或禁用
    formatterMethod ({ state }) {
      const obj = status.find((item) => item.value === +state)
      return obj ? obj.label : '未知'
    },
    // size变化 每页条数发生变化
    handleSizeChange (val) {
      this.requestParameters.page = 1
      this.requestParameters.pagesize = val
      this.getlist()
    },
    // page变化 页码发生变化
    handleCurrentChange (val) {
      this.requestParameters.page = val
      this.getlist()
    },
    // 文章预览
    showview (row) {
      this.articlePreviewshow = true
      this.viewdata = row
    },
    // 删除
    del (id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        remove(id)
        this.getlist()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 新增或者修改弹窗 区分标题
    openshow (row) {
      this.showDialog = true
      this.titleInfo = '新增文章'
    },
    // 序号
    indexMethod (index) {
      return (this.requestParameters.page - 1) * this.requestParameters.pagesize + index + 1
    },
    // 修改文章的功能
    handleEdit (row) {
      this.showDialog = true
      this.titleInfo = '修改文章'
      this.$refs.Edit.Editshow(row)
    },
    // 视频
    handleClick (url) {
      this.videoShow = true
      // this.$refs.video.play()
      this.url = url
    },
    // 关闭视频
    closeVideo () {
      // this.$refs.video.pause()
      this.url = null
      this.videoShow = false
    },
    // 新增文章重新获取列表
    reqgetlist () {
      this.requestParameters.page = 1
      this.getlist()
    }
  }
}
</script>

<style scoped lang='less'>

  .el-icon-film{
    color: blue;
    font-size: 18px;

  }

.box {
  position: fixed;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background: rgba(0,0,0,.3);
  overflow: hidden;
  z-index: 9999;
  .top  {
    position: relative;
    width: 100%;
    height: 100%;
    video {
    width: 600px;
    height: 400px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%);
    }
    .close {
    width: 40px;
    height: 40px;
    color: #fff;
    text-align: center;
    line-height: 40px;
    font-size: 20px;
    background-color:rgba(0,0,0,0.4);
    border-radius: 50px;
    position: absolute;
    top: 15%;
    left: 50%;
    transform: translate(-50%,-50%);
    }
  }
}
</style>
